﻿using CRMG.ConfigurationManager;
using Guru.ErrorHandling.Source.BO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Guru.ErrorHandling
{
    public class LogEntry
    {
        private List<string> db_List = new List<string>() { "db", "database", "dbase", "todb" };
        private List<string> file_list = new List<string>() { "tofile", "file", "fl", "default" };
        string _saveType = "db";

        private string SaveType
        {
            get
            {
                try
                {
                    return Configuration.AppSets["log-type"].ToString();
                }
                catch (Exception)
                {
                    return "file";
                }
            }
            set { _saveType = value; }
        }

        string _filePath = "C:/CrmGuru/log/log.txt";

        private string FilePath
        {
            get
            {
                try
                {
                    return Configuration.AppSets["log-path"].ToString();
                }
                catch (Exception)
                {
                    return "C:/CrmGuru/log/log.txt";
                }
            }
            set { _filePath = value; }
        }

        public LogEntry()
        {
            LogTime = DateTime.Now;
        }

        public LogEntry(int userId)
        {
            UserId = userId;
            LogTime = DateTime.Now;
        }

        private int _OBJID;
        private string _Message;
        private string _StackTrace;
        private string _Title;
        private int _ErrorCode;
        private int _UserId;
        private DateTime _LogTime;

        public int OBJID
        {
            get { return _OBJID; }
            set { _OBJID = value; }
        }
        public string Message
        {
            get { return _Message; }
            set { _Message = value; }
        }
        public string StackTrace
        {
            get { return _StackTrace; }
            set { _StackTrace = value; }
        }
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        public int ErrorCode
        {
            get { return _ErrorCode; }
            set { _ErrorCode = value; }
        }
        public int UserId
        {
            get { return _UserId; }
            internal set { _UserId = value; }
        }
        public DateTime LogTime
        {
            get { return _LogTime; }
            internal set { _LogTime = value; }
        }

        public void Write()
        {
            try
            {
                if (db_List.IndexOf(SaveType) != -1)
                {
                    write2Db();
                }
                else
                {
                    write2File();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void write2File()
        {
            try
            {
                FileMode fMode = File.Exists(FilePath) ? FileMode.Append : FileMode.OpenOrCreate;

                using (StreamWriter outfile = new StreamWriter(new FileStream(FilePath, fMode)))
                {
                    StringBuilder strBuilder = new StringBuilder();
                    strBuilder.AppendFormat("Title : {0}", _Title).AppendLine();
                    strBuilder.AppendFormat("Error Code : {0}", _ErrorCode).AppendLine();
                    strBuilder.AppendFormat("Message : {0}", _Message).AppendLine();
                    strBuilder.AppendFormat("Stack Trace : {0}", _StackTrace).AppendLine();
                    strBuilder.AppendFormat("Log Time : {0}", _LogTime.ToString("yyyy-MM-dd HH:mm:ss")).AppendLine();
                    strBuilder.AppendLine("/**************/");
                    outfile.Write(strBuilder.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void write2Db()
        {
            try
            {
                Log log = new Log();
                log.Title = _Title;
                log.ErrorCode = _ErrorCode;
                log.LogTime = _LogTime;
                log.Message = _Message;
                log.StackTrace = _StackTrace;
                log.UserId = _UserId;
                log.Insert();
            }
            catch (Exception)
            {
                write2File();
            }
        }
    }
}
